import { Component, OnInit } from '@angular/core';
import {FrozenConfig} from '../../../entity/eureka-manage/frozen-config';
import {EurekaFrozenService} from '../../../services/eureka-manage/eureka-fronzen.service';
import * as _ from 'lodash';
import {EurekaApplication} from '../../../entity/eureka-manage/eureka-application';
import {NzNotificationService} from 'ng-zorro-antd/notification';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {FormUtils} from '../../../utils/form-utils';

@Component({
  selector: 'app-frozen',
  templateUrl: './frozen.component.html',
  styleUrls: ['./frozen.component.less']
})
export class FrozenComponent implements OnInit {

  /**
   * 左侧冻结配置列表
   */
  frozenConfigList: FrozenConfig[];
  /**
   * 右侧选中的冻结配置详情
   */
  currentFrozenConfig: FrozenConfig;
  /**
   * 创建配置的开关
   */
  createFrozenConfigModalIsVisible: boolean;
  /**
   * 创建配置的表单
   */
  createFrozenConfigForm: FormGroup;

  constructor(
    private eurekaFrozenService: EurekaFrozenService,
    private notificationService: NzNotificationService,
    private fb: FormBuilder
  ) { }

  ngOnInit() {
    this.fetchFrozenConfigList();
  }

  /**
   * 调用接口获取冻结配置列表
   */
  fetchFrozenConfigList = () => {
    this.eurekaFrozenService.listFrozenConfig().subscribe(data => this.frozenConfigList = data);
  }
  /**
   * 初始化创建冻结配置表单
   */
  initCreateFrozenConfigForm = () => {
    this.createFrozenConfigForm = this.fb.group({
      eurekaCode: [null, [Validators.required]],
    });
  }

  /**
   * 给卡片渲染用的分组函数
   * @param frozenConfig 冻结配置
   * @return 分组好的每个配置下的app列表
   */
  generateAppDisplayGroup = (frozenConfig: FrozenConfig): EurekaApplication[][] => _.chunk(frozenConfig.applicationList, 3);

  /**
   * 点击左侧某个冻结配置时触发更新右侧详情
   * @param frozenConfig 选中的冻结配置
   */
  selectCurrentFrozenConfig = (frozenConfig: FrozenConfig) => {
    this.eurekaFrozenService.getConfigByEurekaCode(frozenConfig.eurekaCode).subscribe(data => {
      if (!!data) {
        this.currentFrozenConfig = data;
      } else {
        this.notificationService.error('Invalid Eureka Code!', '');
        this.currentFrozenConfig = null;
      }
    });
  }
  /**
   * 添加冻结配置按钮点击Handler
   */
  addFrozenConfigButtonOnClick = () => {
    this.initCreateFrozenConfigForm();
    this.createFrozenConfigModalIsVisible = true;
  }

  /**
   * 添加冻结配置弹窗取消Handler
   */
  handleCreateFrozenConfigModalCancel = () => {
    this.createFrozenConfigModalIsVisible = false;
  }

  /**
   * 添加冻结配置弹窗确定Handler
   */
  handleCreateFrozenConfigModalOk = () => {
    // 数据校验
    FormUtils.triggerFormValidate(this.createFrozenConfigForm);
    if (!this.createFrozenConfigForm.valid) {
      return;
    }
    // 转换数据
    const formValue = this.createFrozenConfigForm.getRawValue();
    const newFrozenConfig = {
      ...formValue
    } as FrozenConfig;
    // 调用接口, 在回调中处理后事
    this.eurekaFrozenService.saveFrozenConfig(newFrozenConfig)
      .subscribe(() => {
        this.fetchFrozenConfigList();
        this.createFrozenConfigModalIsVisible = false;
      });
  }

  /**
   * 调用接口移除当前选中的冻结配置
   */
  removeCurrentFrozenConfig = () => {
    if (!this.currentFrozenConfig?.eurekaCode) {
      return;
    }
    this.eurekaFrozenService.removeFrozenConfigByCode(this.currentFrozenConfig.eurekaCode)
      .subscribe(() => {
        this.currentFrozenConfig = null;
        this.fetchFrozenConfigList();
      });
  }
}
